﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skybot.Base.Pool
{
    /// <summary>
    /// 基本数据池
    /// </summary>
    public class BasePool<T>:IPool
        where T : class ,new()
    {
        /// <summary>
        /// 默认初始化1000个对像
        /// </summary>
        /// <param name="initPoolSize">初始对像大小</param>
        /// <param name="maxCount">最大对像大小</param>
        /// <param name="OutOfMaxPrintDelInfo">超过最大对像数后,是否打印删除记录日志</param>
        public BasePool(int initPoolSize = 1000, int maxCount = int.MaxValue-1000,bool OutOfMaxPrintDelInfo=true)
        {
            queue.MaxQueue = maxCount;
            queue.OutOfMaxPrintDelInfo = OutOfMaxPrintDelInfo;
            for (int i = 0; i < 1000; i++)
            {
                queue.Push(new T());
            }
        }

        /// <summary>
        /// 基础队列数据对像
        /// </summary>
        private BaseQueue<T> queue = new BaseQueue<T>();

        /// <summary>
        /// 当前数据池大小
        /// </summary>
        public int PoolCount { get { return queue.QueueCount; }}

        /// <summary>
        /// 还池
        /// </summary>
        /// <param name="obj">从池中取出来的对像</param>
        public void GaveBack(T obj)
        {
            queue.Push(obj);
        }

        /// <summary>
        /// 取池,如果池中的数据够用,则创建一个新对像
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            var po = queue.Pop();
            return po == null ? new T() : po;
        }

        /// <summary>
        /// 将对像归还到所属池中
        /// </summary>
        /// <param name="bakcObject">用于归还的对像,这里应该是 如StateObject 这样的数据对像实例</param>
       public  void Push(object bakcObject)
        {
            GaveBack((T)bakcObject);
        }

    }
}
